package cn.easybuy.web.pre;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.easybuy.dao.ProductCategoryDao;
import cn.easybuy.dao.impl.ProductCategoryDaoImpl;
import cn.easybuy.entity.ProductCategory;

public class MenuServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String opr=request.getParameter("opr");
		PrintWriter out=response.getWriter();
		ProductCategoryDao pcd=new ProductCategoryDaoImpl();
		if (opr.equals("first")) { //获取1级菜单
			List<ProductCategory> list1=pcd.getProductCategoriesByType(1);
			String list1JSON=JSON.toJSONString(list1);
			out.print(list1JSON);
		}else if (opr.equals("second")) {
			String yijiid=request.getParameter("yijiid");
			List<ProductCategory> list2=pcd.getProductCategoriesByParentId(Integer.parseInt(yijiid));//得到2级菜单信息
			StringBuffer erSanJi=new StringBuffer("");
			for (ProductCategory pc : list2) {
				erSanJi.append("<div class=\"zj_l_c\" ><h2>"+pc.getName()+"</h2>");
				//根据2级菜单的id获取3级菜单信息
				List<ProductCategory> list3=pcd.getProductCategoriesByParentId(pc.getId());//3级菜单
				for (ProductCategory pc2 : list3) {
					erSanJi.append("<a href='ProductKindServlet?id3="+pc2.getId()+"&name3="+pc2.getName()+"' id='"+pc2.getId()+"' name='"+pc2.getName()+"' class='product3_'>"+pc2.getName()+"</a>|");
				}
				erSanJi.append("</div>");
			}
			out.print(erSanJi);
		}
		out.flush();
		out.close();
	}

}
